/**
 * This is a representation of a room for use within a maze.
 * 
 * !!!!! Do not modify this file, except for the getNeighbors method. !!!!!
 */

public class Room implements Comparable<Room>
{
	
	// The number of dimensions in a coordinate tuple
	private int dims;
	// The coordinate tuple of the room
	private int[] coords;
	
	/**
	 * Initialize a room with the given coordinates
	 * @param coords the room's coordinates
	 */
	public Room(int[] coords)
	{
		if (coords == null || coords.length == 0)
			throw new IllegalArgumentException("Illegal room");
		
		this.dims = coords.length;
		this.coords = coords;
	}
	
	/**
	 * getDims -- get the total number of dimensions
	 * @return the number of dimensions
	 */
	public int getDims()
	{
		return dims;
	}
	
	/**
	 * getDim -- get the coordinate in the specified dimension
	 * @param d the dimension
	 * @return the coordinate in the dimension
	 */
	public int getDim(int d)
	{
		if (d < 0 || d >= dims)
			throw new IllegalArgumentException("Illegal dimension");
		
		return coords[d];
	}
	
	/**
	 * getNeighbors -- get all nodes that are within one step in each
	 *   dimension of the current room
	 * @return the list of rooms as an implicit linked list
	 */
	public Node<Room> getNeighbors()
	{
		Node<Room> ret = null;
		Node<Room> tmp = null;
		
		// we do not consider the coords below 0, or exceed some boundary, because connection
		// will take care of them!!!
		for(int i=0; i < this.getDims(); i++){
			// forward one step at one dimension
			int[] add = coords.clone();
			add[i] += 1;
			tmp = new Node<Room>(new Room(add),ret);
			ret = tmp;
			
			// backward one step at one dimension
			int[] sub = coords.clone();
			sub[i] -= 1;
			tmp = new Node<Room>(new Room(sub),ret);
			ret = tmp;		
		}
		return ret;
		//throw new RuntimeException("You must implement this method!");
	}
	
	public boolean equals(Object obj)
	{
		if (!(obj instanceof Room))
			return false;
		
		Room r = (Room)obj;
		if (r.getDims() != getDims())
			return false;
		
		for (int d = 0; d < dims; d++)
		{
			if (r.getDim(d) != getDim(d))
				return false;
		}
		return true;
	}
	
	public int compareTo(Room r)
	{
		if (r == null)
			return 1;
		if (r.equals(this))
			return 0;
		
		int sum = 0, d;
		for (d = 0; d < getDims(); d++)
			sum += getDim(d);
		for (d = 0; d < r.getDims(); d++)
			sum -= r.getDim(d);
		
		return (sum != 0 ? sum : 1);
	}
	
	public String toString()
	{
		String out = "(" + coords[0];
		for (int k = 1; k < dims; k++)
			out += ", " + coords[k];
		return out + ")";
	}
	
}
